/**
 * Created by user on 2015/9/14.
 */
var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');
var community = mongoose.model('community');

exports.lotteryListService = function(req ,callback) {

    //var currentPage = 1;
    //if (input.currentPage != '' && input.currentPage != null) {
    //    currentPage = input.currentPage;
    //}
    //var pageSize = 10;
    //var offset = (currentPage - 1) * pageSize;
    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var getSql = function (type) {
        var sql = "select distinct " + type + " from lottery_activity as la left join lottery_activity_community_map as lacm on la.id = lacm.lottery_id";
        return sql;
    }

    var communityName = body.communityName;
    var activityName = body.activityName;

    var communityList = new Array();

    async.series([

        function (callbackInner) {
            community.find({communityName: {$regex: communityName}}, '_id', function (err, cy) {
                if (cy != null && cy.length > 0) {
                    for (var i = 0; i < cy.length; i++) {
                        communityList.push("'" + cy[i]._id + "'");
                    }
                }
                callbackInner();
            });
        },
        function (callbackInner) {

            var sql = getSql("la.*") + " where 1 = 1 ";

            if (communityList.length > 0) {
                sql += "and lacm.community_id in (" + communityList.toString() + ") ";
            } else if (communityName != null) {
                sql += "and lacm.community_id in ('not found') "
            }

            if (activityName != null) {
                sql += " and la.activity_name like '%" + activityName + "%'";
            }

            sql += ' order by la.create_time desc limit ' + index + ' ,' + size;

            mysqlUtil.query(sql, function (err, result) {
                callbackInner(err, result);
            });
        },
        function (callbackInner) {
            var sql = getSql(" activity_name ") + " where 1 = 1 ";

            if (communityList.length > 0) {
                sql += "and lacm.community_id in (" + communityList.toString() + ") ";
            } else if (communityName != null) {
                sql += "and lacm.community_id in ('not found') "
            }

            if (activityName != null) {
                sql += " and la.activity_name like '%" + activityName + "%'";
            }

            mysqlUtil.query(sql, function (err, result) {
                var count = result.length;
                var totalPage = Math.ceil(count / size);
                var temp = {count: count, totalPage:totalPage};
                callbackInner(err, temp);
            });
        }
    ], callback)


};

exports.lotteryDeleteService = function(req ,callback){

    var body = req.body;

    var sql = "delete from lottery_activity where id = " + body.lotteryID;

    mysqlUtil.query(sql, callback);
};

exports.getPrizeByLotteryIdService = function (input, callback) {

    var currentPage = 1;
    if (input.currentPage != '' && input.currentPage != null) {
        currentPage = input.currentPage;
    }
    var pageSize = 10;
    var offset = (currentPage - 1) * pageSize;

    var lotteryId = input.lotteryId;

    var getSql = function (type) {
        var sql = "select " + type + " from lottery_prize lp left join lottery_product lp2 on lp.product_id = lp2.id where lottery_id = " + lotteryId;
        return sql;
    }

    async.series([

        function (callbackInner) {

            var sql = getSql("lp.*,lp2.name as prize_name") + " order by prize_position limit " + offset + "," + pageSize;

            mysqlUtil.query(sql, function (err, result) {
                callbackInner(err, result);
            });
        },
        function (callbackInner) {
            var sql = getSql("count(*) as count ");

            mysqlUtil.query(sql, function (err, result) {
                var count = result[0].count;
                var totalPage = Math.ceil(count / pageSize);
                var temp = {count: count, totalPage:totalPage};
                callbackInner(err, temp);
            });
        }
    ], callback)

};

exports.lotteryPrizeAddService = function (input, callback) {
    var productId = input.productId;
    var levelName = input.levelName;
    var prizeCount = input.prizeCount;
    var rewardCount = input.rewardCount;
    var prizeLevel = input.prizeLevel;
    var lotteryId = input.lotteryId;
    var prizePic = input.prizePic;
    var prizePosition = input.prizePosition;
    var prizeProbability = input.prizeProbability;

    logger.info(prizePic);
    logger.info(prizePosition);
    logger.info(prizeProbability);

    var sql = "insert into lottery_prize (lottery_id,product_id,level_name,prize_count,reward_count,prize_level,create_time,prize_pic,prize_position,prize_probability) values"
        + "(" + lotteryId + "," + productId + ",'" + levelName + "'," + prizeCount + "," + rewardCount + "," + prizeLevel + "," + new Date().getTime() + ",'" + prizePic + "'," + prizePosition + "," + prizeProbability + ")";

    mysqlUtil.query(sql, callback);

};



exports.lotteryPrizeDeleteService = function(req ,callback) {

    var body = req.body;
    if (!(body.LotteryPrizeID && body.LotteryPrizeID.length)) {
        callback({domain:"没有LotteryPrizeID"}, null);
    }
    var LotteryPrizeID = body.LotteryPrizeID;
    logger.debug(LotteryPrizeID);


    var temp = "(";
    for(var i = 0, leng = LotteryPrizeID.length;i<leng;i++) {
        if (i == leng - 1) temp = temp + LotteryPrizeID[i];
        else temp = temp + LotteryPrizeID[i] +",";
    }
    temp += ")";

    var sql = "delete from lottery_prize where id in " + temp;
    mysqlUtil.query(sql, callback);

};



exports.lotteryPrizeEditService = function (input, callback) {

    //var prizeId = input.prizeId;
    //var levelName = input.levelName;
    //var prizeCount = input.prizeCount;
    //var rewardCount = input.rewardCount;
    //var prizeLevel = input.prizeLevel;
    //var prizeProbability = input.prizeProbability;
    //var prizePosition = input.prizePosition;
    //var prizePic = input.prizePic;

    var sql = "update lottery_prize set product_id = "+ input.product_id +",prize_probability = " + input.prize_probability + ",prize_position = " + input.prize_position + ", level_name = '" + input.level_name + "' , prize_count = " + input.prize_count + ", reward_count = " + input.reward_count + ",prize_level = " + input.prize_level;
    if (input.prizePic) {
        sql += ", prize_pic = '" + input.prizePic + "'";
    }
    sql += " where id =" + input.lotteryPrizeID;

    logger.debug(sql);
    mysqlUtil.query(sql, callback);

};